package com.fitzone.web.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fitzone.common.result.Result;
import com.fitzone.model.dto.VipPageDTO;
import com.fitzone.model.entity.Course;
import com.fitzone.model.entity.Vip;
import com.fitzone.web.service.VipService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/vip")
@RequiredArgsConstructor
@Api(tags = "VIP管理")
public class VipController {
    private final VipService vipService;

    @PostMapping
    public Result saveVip(@RequestBody Vip vip){
        vipService.save(vip);
        return Result.ok();
    }

    @PutMapping
    public Result updateVip(@RequestBody Vip vip){
        vipService.updateById(vip);
        return Result.ok();
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Long id){
        vipService.removeById(id);
        return Result.ok();
    }

    @GetMapping("/{page}/{limit}")
    public Result<Page> page(VipPageDTO vipPageDTO){
        Page<Vip> pageInfo = new Page(vipPageDTO.getPage(),vipPageDTO.getLimit());
        LambdaQueryWrapper<Vip> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(vipPageDTO.getType() != null,Vip::getType,vipPageDTO.getType())
                .orderByDesc(Vip::getCreateTime);
        vipService.page(pageInfo, queryWrapper);
        return Result.ok(pageInfo);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        Vip vip = vipService.getById(id);
        return Result.ok(vip);
    }
}
